package com.lin.operatorlogrecord.unit;

import com.lin.operatorlogrecord.entity.enums.IEnumBase;
import com.lin.operatorlogrecord.exception.OperatorLogException;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @Author: chenghao.lin
 * @Date: 2023/09/25
 * @Description:
 */
public class EnumUtils {

    public static IEnumBase getIEnumBase(Class<?> clazz, Integer code) {
        Object[] enumConstants = clazz.getEnumConstants();
        try {
            Method method = clazz.getDeclaredMethod("convertIEnumBase", Integer.class);
            return (IEnumBase) method.invoke(enumConstants[0], code);
        } catch (NoSuchMethodException e) {
            throw new OperatorLogException("该枚举没有ConvertIEnumBase方法");
        } catch (IllegalAccessException | InvocationTargetException e) {
            throw new OperatorLogException("调用反射方法失败");
        }
    }
}
